<?php
	
	/**
	 * get all patient profiles
	 */
	$app->get('/profilePatient', authorize('user'), function () use ($app, $db) {
		$profilePatients = array();
		$rs = $db->ProfilePatient();
		if( $_GET["userId"] )
		{
			$rs = $rs->where("userId", $_GET["userId"] );
		}
		foreach ( $rs as $profilePatient)
		{
			$profilePatients[] = array (
				"id" => $profilePatient["id"],
				"userId" => $profilePatient["userId"],
				"healthNumber" => $profilePatient["healthNumber"]
			);
		}
		
		$app->response()->header("Content-Type", "application/json");
		echo json_encode( $profilePatients );
	});
	
	/**
	 * get profilePatient by id
	 */
	$app->get('/profilePatient/:id', authorize('user'), function ($id) use ($app, $db) {
		$profilePatient = $db->ProfilePatient()->where("id", $id)->fetch();
		$profilePatient = array (
				"id" => $profilePatient["id"],
				"userId" => $profilePatient["userId"],
				"healthNumber" => $profilePatient["healthNumber"]
		);
		
		$app->response()->header("Content-Type", "application/json");
		echo json_encode($profilePatient);
	});
	
	/**
	 * create profilePatient
	 */
	$app->post('/profilePatient', authorize('user'), function () use ($app, $db) {
		$profilePatient = json_decode( $app->request()->getBody(), true );
		$profilePatient = $db->ProfilePatient()->insert($profilePatient);
		$profilePatient = array (
			"id" => $profilePatient["id"],
			"userId" => $profilePatient["userId"],
			"healthNumber" => $profilePatient["healthNumber"]
		);
		
		$app->response()->header("Content-Type", "application/json");
		echo json_encode($profilePatient);
	});
	
	
	/**
	 * update ProfilePatient
	 */
	$app->put('/profilePatient/:id', authorize('user'), function ($id) use ($app, $db) {
		$profilePatient = json_decode( $app->request()->getBody(), true );
		$db->ProfilePatient("id",$id)->update($profilePatient);
		$profilePatient = array (
			"id" => $profilePatient["id"],
			"userId" => $profilePatient["userId"],
			"healthNumber" => $profilePatient["healthNumber"]
		);
		
		$app->response()->header("Content-Type", "application/json");
		echo json_encode($profilePatient);
	});
	
	
	/**
	 * delete profilePatient
	 */
	$app->delete('/profilePatient/:id', authorize('user'), function ($id) use ($app, $db) {
		$response = $db->ProfilePatient("id",$id)->delete();
		
		$app->response()->header("Content-Type", "application/json");
		echo json_encode($response);
	});
/*	
*/
		
?>